﻿using System;
using System.Data;
using System.Text;
using System.Data.OleDb;
using ihongma.DBUtility;
using System.Collections.Generic;//Please add references
namespace ihongma.DAL
{
	/// <summary>
	/// 数据访问类:web_photo
	/// </summary>
	public partial class web_photo
	{
		public web_photo()
		{}
		#region  Method
        /// <summary>
        /// 得到最大ID
        /// </summary>
        public int GetMaxId(OleDbConnection conn, OleDbTransaction trans)
        {
            string strSql = "select top 1 id from dt_web_photo order by id desc";
            object obj = DbHelperOleDb.GetSingle(conn, trans, strSql);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return int.Parse(obj.ToString());
            }
        }

		/// <summary>
		/// 得到最大ID
		/// </summary>
		public int GetMaxId()
		{
		return DbHelperOleDb.GetMaxID("id", "dt_web_photo"); 
		}

		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(int id)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from dt_web_photo");
			strSql.Append(" where id=@id");
			OleDbParameter[] parameters = {
					new OleDbParameter("@id", OleDbType.Integer,4)
};
			parameters[0].Value = id;

			return DbHelperOleDb.Exists(strSql.ToString(),parameters);
		}


		/// <summary>
		/// 增加一条数据
		/// </summary>
		public bool Add(ihongma.Model.web_photo model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("insert into dt_web_photo(");
			strSql.Append("photo_id,big_img,small_img)");
			strSql.Append(" values (");
			strSql.Append("@photo_id,@big_img,@small_img)");
			OleDbParameter[] parameters = {
					new OleDbParameter("@photo_id", OleDbType.Integer,4),
					new OleDbParameter("@big_img", OleDbType.VarChar,255),
					new OleDbParameter("@small_img", OleDbType.VarChar,255)};
			parameters[0].Value = model.photo_id;
			parameters[1].Value = model.big_img;
			parameters[2].Value = model.small_img;

			int rows=DbHelperOleDb.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(ihongma.Model.web_photo model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update dt_web_photo set ");
			strSql.Append("photo_id=@photo_id,");
			strSql.Append("big_img=@big_img,");
			strSql.Append("small_img=@small_img");
			strSql.Append(" where id=@id");
			OleDbParameter[] parameters = {
					new OleDbParameter("@photo_id", OleDbType.Integer,4),
					new OleDbParameter("@big_img", OleDbType.VarChar,255),
					new OleDbParameter("@small_img", OleDbType.VarChar,255),
					new OleDbParameter("@id", OleDbType.Integer,4)};
			parameters[0].Value = model.photo_id;
			parameters[1].Value = model.big_img;
			parameters[2].Value = model.small_img;
			parameters[3].Value = model.id;

			int rows=DbHelperOleDb.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public DbHelperAge Delete(int id)
		{
            DbHelperAge age = new DbHelperAge();
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from dt_web_photo ");
            strSql.Append(" where photo_id=@id");
			OleDbParameter[] parameters = {
					new OleDbParameter("@id", OleDbType.Integer,4)
};
			parameters[0].Value = id;

            age.OleDbParameterList = parameters;
            age.StrSql = strSql.ToString();
            return age;
		}
		/// <summary>
		/// 批量删除数据
		/// </summary>
		public bool DeleteList(string idlist )
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from dt_web_photo ");
			strSql.Append(" where id in ("+idlist + ")  ");
			int rows=DbHelperOleDb.ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public ihongma.Model.web_photo GetModel(int id)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select id,photo_id,big_img,small_img from dt_web_photo ");
			strSql.Append(" where id=@id");
			OleDbParameter[] parameters = {
					new OleDbParameter("@id", OleDbType.Integer,4)
};
			parameters[0].Value = id;

			ihongma.Model.web_photo model=new ihongma.Model.web_photo();
			DataSet ds=DbHelperOleDb.Query(strSql.ToString(),parameters);
			if(ds.Tables[0].Rows.Count>0)
			{
				if(ds.Tables[0].Rows[0]["id"]!=null && ds.Tables[0].Rows[0]["id"].ToString()!="")
				{
					model.id=int.Parse(ds.Tables[0].Rows[0]["id"].ToString());
				}
				if(ds.Tables[0].Rows[0]["photo_id"]!=null && ds.Tables[0].Rows[0]["photo_id"].ToString()!="")
				{
					model.photo_id=int.Parse(ds.Tables[0].Rows[0]["photo_id"].ToString());
				}
				if(ds.Tables[0].Rows[0]["big_img"]!=null && ds.Tables[0].Rows[0]["big_img"].ToString()!="")
				{
					model.big_img=ds.Tables[0].Rows[0]["big_img"].ToString();
				}
				if(ds.Tables[0].Rows[0]["small_img"]!=null && ds.Tables[0].Rows[0]["small_img"].ToString()!="")
				{
					model.small_img=ds.Tables[0].Rows[0]["small_img"].ToString();
				}
				return model;
			}
			else
			{
				return null;
			}
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public List<Model.web_photo> GetList(int id)
		{
            List<Model.web_photo> model=new List<ihongma.Model.web_photo>();
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select id,photo_id,big_img,small_img ");
			strSql.Append(" FROM dt_web_photo ");
			if(id>0)
			{
                strSql.Append(" where photo_id=" + id);
			}
           DataSet dt=DbHelperOleDb.Query(strSql.ToString());
           if (dt.Tables[0].Rows.Count > 0)
           {
               foreach (DataRow p in dt.Tables[0].Rows)
               {
                   Model.web_photo models = new ihongma.Model.web_photo();
                   if (p["id"] != null && p["id"].ToString() != "")
                   {
                       models.id = int.Parse(p["id"].ToString());
                   }
                   if (p["photo_id"] != null && p["photo_id"].ToString() != "")
                   {
                       models.photo_id = int.Parse(p["photo_id"].ToString());
                   }
                   if (p["big_img"] != null && p["big_img"].ToString() != "")
                   {
                       models.big_img = p["big_img"].ToString();
                   }
                   if (p["small_img"] != null && p["small_img"].ToString() != "")
                   {
                       models.small_img = p["small_img"].ToString();
                   }
                   models.Taglist = new tag().GetList("photoid=" + p["id"].ToString());
                   model.Add(models);
               }
           }
			return model;
		}

		/*
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		{
			OleDbParameter[] parameters = {
					new OleDbParameter("@tblName", OleDbType.VarChar, 255),
					new OleDbParameter("@fldName", OleDbType.VarChar, 255),
					new OleDbParameter("@PageSize", OleDbType.Integer),
					new OleDbParameter("@PageIndex", OleDbType.Integer),
					new OleDbParameter("@IsReCount", OleDbType.Boolean),
					new OleDbParameter("@OrderType", OleDbType.Boolean),
					new OleDbParameter("@strWhere", OleDbType.VarChar,1000),
					};
			parameters[0].Value = "dt_web_photo";
			parameters[1].Value = "id";
			parameters[2].Value = PageSize;
			parameters[3].Value = PageIndex;
			parameters[4].Value = 0;
			parameters[5].Value = 0;
			parameters[6].Value = strWhere;	
			return DbHelperOleDb.RunProcedure("UP_GetRecordByPage",parameters,"ds");
		}*/

		#endregion  Method
	}
}

